import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/investigation_plan_ledger/investigation_plan_ledger_detail/igation_ledger_detail_controller.dart';

///排查计划详情
class InvestigationPlanLedgerDetailPage extends StatelessWidget {
  InvestigationPlanLedgerDetailPage({super.key});

  final controller = Get.put(InvestigationPlanLedgerDetailController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: '排查计划详情',
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
              color: AppColors.bg_color_grey,
              child: CustomTitleChartPage(
                mainAxisSize: MainAxisSize.min,
                title: controller.hiddenDangerInvestigationLedgerRecords.planName ?? '',
                titleRightWidget: _getResult(controller.hiddenDangerInvestigationLedgerRecords.lateNum),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _rowData('排查类型：', controller.hiddenDangerInvestigationLedgerRecords.inspectionTypeName ?? ''),
                    AppGaps.vGap12,
                    _rowData('排查部门：',
                        '${controller.hiddenDangerInvestigationLedgerRecords.inspectionDepartmentName ?? ''}-${controller.hiddenDangerInvestigationLedgerRecords.inspectionPersonnelName ?? ''}'),
                    AppGaps.vGap12,
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 10.w),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        color: const Color(0x1A5C6DFF),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Column(
                            children: [
                              _topTitle('点位', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.numberOfPoints ?? '0'}',
                                  const Color(0xFF5C6DFF)),
                              AppGaps.vGap10,
                              _topTitle('补检', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.reExaminationNum ?? '0'}',
                                  const Color(0xFFFC9900)),
                            ],
                          ),
                          _divider(),
                          Column(
                            children: [
                              _topTitle('任务', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.taskNum ?? '0'}',
                                  const Color(0xFF5C6DFF)),
                              AppGaps.vGap10,
                              _topTitle('漏检', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.lateNum ?? '0'}',
                                  const Color(0xFFFD6B6B)),
                            ],
                          ),
                          _divider(),
                          Column(
                            children: [
                              _topTitle('隐患', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.numberOfHiddenHazardsNum ?? '0'}',
                                  const Color(0xFFFFCE24)),
                              AppGaps.vGap10,
                              _topTitle('', const Color(0xFF495061), '', const Color(0xFF5C6DFF)),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            _bottomWidget()
          ],
        ),
      ),
    );
  }

  // 落实结果
  Widget _getResult(int? i) {
    switch (i) {
      case 0:
        return _title('已完成', const Color(0xFF33A9FF));
      case 1:
        return _title('漏检', const Color(0xFFFD6B6B));
      case 2:
        return _title('漏检', const Color(0xFFFD6B6B));
      case 3:
        return _title('漏检', const Color(0xFFFD6B6B));
      default:
        return _title('漏检', const Color(0xFFFD6B6B));
    }
  }

  Widget _title(String title, Color color) {
    return Row(
      children: [
        Container(
          width: 52.w,
          height: 18.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: color,
              borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(10),
                topRight: Radius.circular(10),
                bottomRight: Radius.circular(10),
              )),
          child: Text(
            title,
            style: TextStyle(
              color: Colors.white,
              fontSize: 10.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
        ),
      ],
    );
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
            child: Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )),
      ],
    );
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap12,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 16.sp,
            color: square,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 24.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ])),
    );
  }

  ///分割线
  Widget _bigDivider() {
    return Container(
      width: 1.w,
      height: 100.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0x00557CFD),
        Color(0xCC557CFD),
      ])),
    );
  }

  Widget _trouble(Color color, String title, String? value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          title,
          style: TextStyle(
            color: AppColors.font_color_blue,
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        AppGaps.vGap2,
        Text(
          value.toString(),
          style: TextStyle(
            color: color,
            fontWeight: FontWeight.w500,
            fontSize: 16.sp,
          ),
        ),
      ],
    );
  }

  Widget _itemWidget(int index) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Column(
          children: [
            // Container(
            //   width: 10.w,
            //   height: 10.w,
            //   color: Colors.red,
            // ),
            Container(
              width: 10.w,
              height: 10.w,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                  color: _color(index % 4),
                  width: 2.w,
                ),
              ),
            ),
            _bigDivider(),
          ],
        ),
        AppGaps.hGap20,
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 10.w),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: const Color(0xFFF5F6FF),
              border: Border.all(
                color: const Color(0x33587DF5),
              ),
            ),
            child: Column(
              children: [
                Row(
                  children: [
                    AppGaps.hGap12,
                    Text(
                      '${controller.getPlanRecordEntity[index].riskUnitName}',
                      style: TextStyle(color: const Color(0xFF495061), fontWeight: FontWeight.w500, fontSize: 14.sp),
                    ),
                    const Spacer(),
                    Stack(
                      alignment: Alignment.centerLeft,
                      children: [
                        SvgPicture.asset(AppImages.icon_living_body_recognition_bg),
                        Positioned(
                            left: 10,
                            child: Text(
                              '活体识别',
                              style: TextStyle(color: const Color(0xFF5C6DFF), fontWeight: FontWeight.w400, fontSize: 12.sp),
                            ))
                      ],
                    ).click(controller.onClickOnDetailDetail),
                    AppGaps.hGap12,
                  ],
                ),
                AppGaps.vGap10,
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Row(
                      children: [
                        _trouble(const Color(0xFF5C6DFF), '任务', '${controller.getPlanRecordEntity[index].taskNum ?? '0'}'),
                      ],
                    ),
                    Row(
                      children: [
                        _trouble(const Color(0xFFFC9900), '补检', '${controller.getPlanRecordEntity[index].reExaminationNum ?? '0'}'),
                      ],
                    ),
                    Row(
                      children: [
                        _trouble(const Color(0xFFFD6B6B), '漏检', '${controller.getPlanRecordEntity[index].lateNum ?? '0'}'),
                      ],
                    ),
                    Row(
                      children: [
                        _trouble(const Color(0xFFFFCE24), '隐患', '${controller.getPlanRecordEntity[index].dangerNum ?? '0'}'),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
        )
      ],
    );
  }

  Widget _bottomWidget() {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return controller.getPlanRecordEntity.isNotEmpty
              ? Container(
                  padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
                  color: AppColors.bg_color_grey,
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                    child: Container(
                      padding: EdgeInsets.fromLTRB(16.w, 20.w, 16.w, 0.w),
                      child: Column(
                        children: [
                          ListView.builder(
                            physics: const ClampingScrollPhysics(),
                            shrinkWrap: true,
                            itemCount: controller.getPlanRecordEntity.length,
                            itemBuilder: (context, index) {
                              final item = controller.getPlanRecordEntity[index];
                              return _itemWidget(index).click(() {
                                return controller.onClickDetailDetail(item);
                              });
                            },
                          )
                        ],
                      ),
                    ),
                  ),
                )
              : Container();
        });
  }
}

Color _color(int index) {
  switch (index) {
    case 1:
      return const Color(0xFFFD6B6B);
    case 2:
      return const Color(0xFFFC9900);
    case 3:
      return const Color(0xFF5C6DFF);
    default:
      return const Color(0xFFFD6B6B);
  }
}
